﻿using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing;
using System.Runtime.Serialization;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace DarkLight.Model
{
    [DataContract]
    public class Sun : SpaceObject
    {
        #region Constructors
        public Sun(float diameter)
            : base()
        {
            this.Radius = diameter / 2;
        }
        #endregion

        #region Public Members
        [DataMember]
        public float Radius
        {
            get { return _radius; }
            set
            {
                if (_radius != value)
                {
                    RaisePropertyChanging("Radius");
                    _radius = value;
                    RaisePropertyChanged("Radius");
                }
            }
        }
        private float _radius;

        public Bitmap DiffuseTexture
        {
            get { return _diffuseTexture; }
            set
            {
                if (_diffuseTexture != value)
                {
                    RaisePropertyChanging("DiffuseTexture");
                    _diffuseTexture = value;
                    RaisePropertyChanged("DiffuseTexture");
                }
            }
        }
        private Bitmap _diffuseTexture;
        #endregion

        #region Serialization Members
        [DataMember]
        [SuppressMessage("Microsoft.Performance", "CA1811")]
        private byte[] DiffuseTextureByteArray
        {
            get
            {
                if (this.DiffuseTexture == null)
                    return null;
                using (var ms = new MemoryStream())
                {
                    this.DiffuseTexture.Save(ms, ImageFormat.Png);
                    return ms.ToArray();
                }
            }
            set
            {
                if (value != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        ms.Write(value, 0, value.Count());
                        this.DiffuseTexture = new Bitmap(Image.FromStream(ms));
                    }
                }
                else
                    this.DiffuseTexture = null;
            }
        }
        #endregion

        //#region Private Members
        ////private void InitializeComponent()
        ////{
        ////    //_effect = this.CreateEffect(Properties.Resources.Sun);
        ////    //_renderLayout = this.CreateRenderLayout(_effect);

        ////    //float lightMantissa = 4;
        ////    //float nightLogIntensity = 5;
        ////    //float x = lightMantissa * (float)Math.Pow(10.0f, nightLogIntensity);
        ////    //float y = lightMantissa * (float)Math.Pow(10.0f, nightLogIntensity);
        ////    //float z = lightMantissa * (float)Math.Pow(10.0f, nightLogIntensity);
        ////    //float w = 1.0f;
        ////    //this.Intensity = new Vector4(x, y, z, w);
        ////}
        //#endregion
    }
}
